<template>
  <div>
    <!-- 导航栏 -->
    <van-nav-bar title="黑马头条-登录" />

    <!-- 表单 -->
    <van-form @submit="onSubmit">
      <van-field
        v-model="mobile"
        required
        name="mobile"
        label="用户名"
        placeholder="请输入手机号"
        :rules="[{ required: true, message: '请输入手机号' }]"
      />
      <van-field
        v-model="password"
        type="password"
        name="code"
        label="验证码"
        placeholder="请输入6位验证码"
        :rules="[{ required: true, message: '请输入6位验证码' }]"
      />
      <div style="margin: 16px">
        <van-button
          round
          block
          type="info"
          native-type="submit"
          :loading="isLoading"
          :disabled="isLoading"
          loading-text="加载中..."
         >提交</van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
export default {
  name: 'myLogin',
  data () {
    return {
      mobile: '13888888888',
      password: '246810',
      isLoading: false // 加载状态
    }
  },
  methods: {
    async onSubmit (values) {
      try {
        this.isLoading = true
        console.log('submit', values)
        const res = await this.$store.dispatch('getToken', values)
        console.log(res)
        this.$Notify({ type: 'success', message: '登录成功' })
        // this.$router.push('/layout')
        this.$router.replace('/layout')
      } catch (err) {
        this.$Notify({ type: 'danger', message: '登录失败' })
      }
      // 关闭加载按钮
      this.isLoading = false
    }
  }
}
</script>

<style scoped></style>
